﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using 安检系统.Utils;

namespace 安检系统
{
    /// <summary>
    /// CheckWindow.xaml 的交互逻辑
    /// </summary>
    public partial class CheckWindow : Window
    {

        JObject paramObj;
        delegate void MyDelegate(Object value);
        public CheckWindow()
        {
            InitializeComponent();
            this.Loaded += Window_Loaded;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            carno.Text = (String)paramObj.GetValue("carno");
        }
    

        private void Button_Check_Pass(object sender, RoutedEventArgs e)
        {
            if (!(bool)inspectionItems1.IsChecked)
            {
                MessageBox.Show("有未选中的检查项，请确认后通过安检");
                return;
            }
            if (!(bool)inspectionItems2.IsChecked)
            {
                MessageBox.Show("有未选中的检查项，请确认后通过安检");
                return;
            }
            if (!(bool)inspectionItems3.IsChecked)
            {
                MessageBox.Show("有未选中的检查项，请确认后通过安检");
                return;
            }
            if (!(bool)inspectionItems4.IsChecked)
            {
                MessageBox.Show("有未选中的检查项，请确认后通过安检");
                return;
            }
            if (!(bool)inspectionItems5.IsChecked)
            {
                MessageBox.Show("有未选中的检查项，请确认后通过安检");
                return;
            }
            JObject json =new JObject();
            json.Add("checkStatus", "1");
            json.Add("id",paramObj.GetValue("id"));
            json.Add("checkPerson", App.getGlobalVariables().LoginUserName);
            String response = HttpPostHelper.doHttpPost("/parkRecord/safeCheck", json.ToString());

            JObject respJson = JObject.Parse(response);
            bool success = (bool)respJson["success"];
            if (success)
            {
                this.DialogResult = true;
                this.Close();
            }
            else
            {
                string data = respJson["message"].ToString();
                MessageBox.Show(data);
            }
        }

        private void Button_Check_NotPass(object sender, RoutedEventArgs e)
        {
            List<JObject> list = new List<JObject>();
            

            if (!(bool)inspectionItems1.IsChecked)
            {
                JObject jobject = new JObject();
                string reason = (string)inspectionItems1.Content;
                jobject.Add("reason1", reason);
                list.Add(jobject);
            }
            if (!(bool)inspectionItems2.IsChecked)
            {
                JObject jobject = new JObject();
                string reason = (string)inspectionItems2.Content;
                jobject.Add("reason2", reason);
                list.Add(jobject);
            }
            if (!(bool)inspectionItems3.IsChecked)
            {
                JObject jobject = new JObject();
                string reason = (string)inspectionItems3.Content;
                jobject.Add("reason3", reason);
                list.Add(jobject);
            }
            if (!(bool)inspectionItems4.IsChecked)
            {
                JObject jobject = new JObject();
                string reason = (string)inspectionItems4.Content;
                jobject.Add("reason4", reason);
                list.Add(jobject);
            }
            if (!(bool)inspectionItems5.IsChecked)
            {
                JObject jobject = new JObject();
                string reason = (string)inspectionItems5.Content;
                jobject.Add("reason5", reason);
                list.Add(jobject);
            }
            
            JObject json = new JObject();
            json.Add("checkStatus", "2");
            json.Add("id", paramObj.GetValue("id"));
            json.Add("reason", list.ToString());

            String response = HttpPostHelper.doHttpPost("/parkRecord/safeCheck", json.ToString());

            JObject respJson = JObject.Parse(response);
            bool success = (bool)respJson["success"];
            if (success)
            {
                this.DialogResult = true;
                this.Close();
            }
            else
            {
                string data = respJson["message"].ToString();
                MessageBox.Show(data);
            }
        }

        private void Button_return(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void selectAll_OnClick(object sender, RoutedEventArgs e)
        {
            bool flag = allInspectionItems.IsChecked == true ? true : false;
            for (int i = 0; i < checkTabItem.Children.Count; i++)
            {
                var item = checkTabItem.Children[i];
                if (item is CheckBox)
                {
                    CheckBox checkBoxItem = (CheckBox)item;
                    checkBoxItem.IsChecked = flag;
                }
            }
        }

        public void setParam(String paramStr)
        {
            paramObj = JObject.Parse(paramStr);
        }
    }
}
